package com.hzc.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hzc.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117635552";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCzkd8KPy+YmjWUG8ObRG7C0gOdkXaxlUz2p/tX1Y9+uko2cnR/vyXEIaah4fCn1/dYw7CH7xafzEVx4CIT+X5VmnYXGL4gShadhER2ldBlBORKeAul8S74se4PcikdyyglwAehKgNSAED62m9bKIcxGWM72Pp/kKHekqYSTlx1nA1eS2A/AeeHe6o467BzU6RR0ni8/WGKoBigAAL76znuA84va9PvjvKSW5RD4GQXDQ5oA8u5LFtp63rplpZiWpAYLejG4rL0k26FikfZCsDAH7eHmvxqV8oRFoTZSqi5sWeXOdiCU6KZ8XQkun2qTWR7hTFay46qbrBGsBnk0bFJAgMBAAECggEBAKFQ6zjuWJjT+EwMwSXsTUCRoJPeCO4xvQldESvv+9ASFPdizQuyMYBH5sVJD4QJPPiuHBmSDRYTUVOi0Zp2KStH7syC+tJRHI33M/YAUk1eHqRTwcsap2LdnYqcuRk919xJ7dC3yHHTLozszNrsxr6yGva5zxfoW7JyteR+JwuzVcaifE2k0cZFYuTDqrwphtzYrB/ork0XLVcp2sGP7LywSaW6/Go2XoviShrg5IbQ7VZkX+gu07XgDpykMT5v1AgDkldMgy6eqQVzygdVEvXCb1nGVNKQwAjoZ/z7UXHXeCDDkvqZcnflEJUfRLUfZVr1Tvr9izMKyLnz4ssa1YECgYEA/OEYNymRhnGMi69Z1JVzRVrgCmDlq8fKLD5wnOubLHvcAv79Rrk0J+bUpgTj8BgTbnfrW50lpOpLA4cM95KYN966PbmXbpFpInshWzLt6c9q3EocSB5QWpUQJ7crPKGiEkIWoEk4OW1hFI7eytTMyXC3I2DjibMUWieLWa89a7ECgYEAtcksutJXaPtKvmXrusKiRRmWNLwkCCkOJwCB8P24oTHm7QW9DsHaua9T/vWzoLWyhJ8B2QZP/3H2IeM9nCx/RYyzIyHbdbwFcph6sYIv+qX7I+FYUxN9xxfo+8aH8TsN9rpVnKpm3ETyQgRPy87uifp1B6gvHsmgmHaVRkb5PRkCgYBlrGM+GpqGQbVOnGwzyvsT0Zvu0FLHpIanD1vb2+cCFbK6Cafv/Zr4yBiLv7+d0zBM5C5fIWn3SrRqcKxIBkrpe8XJ94rpS9HHqVYaAvohXeILPEjuvjF0miZqsXzS3fH6AVRzkbznahD0YRU23CSSgZ1Avj3+KWBvdyLSJmE9oQKBgQCyKGB7FAxwqd8d7LtzhK8N876fHkhd6waguMGg0rhw2nxMjVQ1M03Or4Mtm9WYypoTmdZwwNwEKMclfRTzizSMkBE8fEEeCNB59/A/Ka0mBCe2iXJLsQ2S4t8GahLvG4Za6VFgQiye8b9PbfRadQqZFWo+v9i2XrrS0V0IzCQWeQKBgDtJG3kWHoYti1ueppVv6q+c3PF5fMkI4GOFdGVypx2WXipKRY7NX/BfttC/gWmhVauE2C4xkFj4Zz2EUAyE/EGiTOlLqAuDiyTPK35upgmNcgXbjQQSU1nGylNCtJnZl0y9REYdW6OCxK5YXxSgPql1VQWXgtBhQDZoP/mDF1fp";
	// 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsqNb4jLbznLYkXWYxR9wIIBsI/Q8OMZcV0bjbrrgm69Oxu1wtXO1v+r8hXVgWna0aUkn5IWVxWoIP62SnycB64xtEQTfxYw8KYpQZakAY8d2KlcSIdzEmN1pXhd2Z8rc/5tYJVUpUKD22a+or8E7l5VNONLH5im/Az1vc0LAjpVaU4jvwad23kcZTetPeWIAa93WcXSVdH7kcv00E8GaclF+ci3XCh0eAOWelD36jHr7nAhA2mRtoMqGRq7QWwNcm6Xah7viZjzbTvexrHoEfylgYu8qEhdS1ZaWhJ0qZYc1WvbC1Z4S1xoMYMcsBtNl/77jw0E1ZJinetqCT/gBuwIDAQAB";
	// 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://38e630e156.qicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 自动关单时间
    private String timeout = "15m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // 30分钟内不付款就会自动关单
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return result;
    }
}
